import ServiceExtensionAbility from '@ohos.application.ServiceExtensionAbility';
import window from '@ohos.window';
import display from '@ohos.display';
import commonEvent from '@ohos.commonEvent';

const BG_COLOR = '#00000000';
var g_powerWindowFirst = undefined;

export default class PowerServiceExtAbility extends ServiceExtensionAbility {
    /**
     * Lifecycle function, called back when a service extension is started for initialization.
     */
    onCreate(want) {
        console.log('PowerServiceExtAbility onCreate' + want.abilityName);
        globalThis.extensionContext = this.context;
        globalThis.g_powerWindowFirst = g_powerWindowFirst;

        commonEvent.createSubscriber({events: ["KEY_BACK", "KEY_HOME", "KEY_SETUP", "KEY_SETUPMENU"]})
            .then((commonEventSubscriber) => {
                console.log("=-= createSubscriber success, commonEventSubscriber: " +JSON.stringify(commonEventSubscriber));
                commonEvent.subscribe(commonEventSubscriber, (err, data) => {
                    console.log("=-= createSubscriber success, commonEventSubscriber: " +JSON.stringify(commonEventSubscriber));
                    if(err.code !== 0){
                        console.log("=-= subscribe err, err: " +JSON.stringify(err));
                        return;
                    }
                    console.log("=-= PowerServiceExtAbility subscribe, data: " +JSON.stringify(data.event));
                    globalThis.powerWindow.destroy();
                    globalThis.g_powerWindowFirst = undefined;
                    globalThis.extensionContext.terminateSelf();
                })
            })
            .catch((err) => {
                console.log("=-= createSubscriber fail, err: "+ JSON.stringify(err));
            })
    }

    /**
     * Lifecycle function, called back when a service extension is started or recall.
     */
    onRequest(want, startId) {
        globalThis.abilityWant = want;
        console.log('PowerServiceExtAbility onRequest. start id is ' + startId);
        console.log('want: ' + JSON.stringify(want));
        display.getDefaultDisplay().then(dis => {
            let navigationBarRect = {
                left: 0,
                top: 0,
                width: dis.width,
                height: dis.height
            };
            this.createWindow("Power Dialog" + startId, window.WindowType.TYPE_FLOAT, navigationBarRect);
        })
    }

    /**
     * Lifecycle function, called back before a service extension is destroyed.
     */
    onDestroy() {
        console.log('PowerServiceExtAbility onDestroy.');
    }

    private async createWindow(name: string, windowType: number, rect) {
        try {
            if (globalThis.g_powerWindowFirst != undefined) {
                console.log('destroy first power window');
                globalThis.g_powerWindowFirst.destroy();
                globalThis.g_powerWindowFirst = undefined;
            }
            const powerWin = await window.create(globalThis.extensionContext, name, windowType);
            if (globalThis.g_powerWindowFirst == undefined) {
                g_powerWindowFirst = powerWin;
                globalThis.g_powerWindowFirst = g_powerWindowFirst;
            }
            globalThis.powerWindow = powerWin;
            await powerWin.moveTo(rect.left, rect.top);
            await powerWin.resetSize(rect.width, rect.height);
            await powerWin.loadContent('pages/powerDialog');
            await powerWin.setBackgroundColor(BG_COLOR);
            await powerWin.show();
            console.log('Power window create success');
        } catch {
            console.log('Power window create failed');
        }
    }
}